/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visual;

import Logica.Severidad;
import Persistencia.exceptions.PreexistingEntityException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDesktopPane;

/**
 *
 * @author Alberto
 */
public class frmAltaAnomalia extends javax.swing.JInternalFrame {

    ControladoraVisual miVisual = new ControladoraVisual();
    DefaultComboBoxModel modeloSeveridad = new DefaultComboBoxModel();
    /**
     * Creates new form frmAltaAnomalia
     */
    JDesktopPane miEscritorio;
    
    public frmAltaAnomalia(ControladoraVisual miVisual, JDesktopPane miEscritorio) {
        initComponents();
        this.cargarCombo();
        this.cmbSeveridades.setModel(modeloSeveridad);
        this.miVisual = miVisual;
        this.miEscritorio = miEscritorio;
    }
    
    private void cargarCombo(){
        List<Severidad> severidades = miVisual.dameSeveridades();
        for (Severidad unaSeveridad:severidades)
            this.modeloSeveridad.addElement(unaSeveridad);
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSalir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cmbSeveridades = new javax.swing.JComboBox();
        btnAltaSeveridad = new javax.swing.JButton();
        btnAltaAnomalia = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        setTitle("Alta Anomalia");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel1.setText("Ingrese la anomalía");

        txtNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreActionPerformed(evt);
            }
        });

        jLabel2.setText("Severidad de la anomalía");

        cmbSeveridades.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbSeveridades.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSeveridadesActionPerformed(evt);
            }
        });

        btnAltaSeveridad.setText("Nueva Severidad");
        btnAltaSeveridad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaSeveridadActionPerformed(evt);
            }
        });

        btnAltaAnomalia.setText("Alta Anomalía");
        btnAltaAnomalia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaAnomaliaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAltaAnomalia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSalir))
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmbSeveridades, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnAltaSeveridad))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbSeveridades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAltaSeveridad)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnAltaAnomalia))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombreActionPerformed

    private void cmbSeveridadesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSeveridadesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbSeveridadesActionPerformed

    private void btnAltaSeveridadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaSeveridadActionPerformed
        frmAltaSeveridad unaSeveridad = new frmAltaSeveridad(miVisual,modeloSeveridad);
        miEscritorio.add(unaSeveridad);
        unaSeveridad.setMaximizable(true);
        unaSeveridad.setClosable(true);
        unaSeveridad.setVisible(true);
    }//GEN-LAST:event_btnAltaSeveridadActionPerformed

    private void btnAltaAnomaliaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaAnomaliaActionPerformed
        String nombre = txtNombre.getText();
        try {
            Severidad unaSeveridad = (Severidad)this.cmbSeveridades.getSelectedItem();
            miVisual.altaAnomalia(nombre,unaSeveridad);
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(frmAltaAnomalia.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(frmAltaAnomalia.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
    }//GEN-LAST:event_btnAltaAnomaliaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAltaAnomalia;
    private javax.swing.JButton btnAltaSeveridad;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbSeveridades;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
